---
layout: base
title: Query
---

<header class="jumbotron subhead" id="overview">
  <div class="container">
    <h3>Query Builder (Beta)</h3>
  </div>
</header>

<div class="container">
  <div class="row">
    <div class="col-md-3">
      <div id="sidebar" role="complementary" data-spy="affix" data-offset-top="80">
        <ul class="nav sidebar">
          <li class="active"><a href="#define">Overview</a></li>
        </ul>
      </div>
    </div>
    <div class="col-md-9">

      <section id="define">
        <div class="page-header">
          <h1>1. Overview</h1>
        </div>
        <p class="lead">The default way to query currently is by using the find method, where you pass the where clause and the arguments. It follows the same conventions as <a href="http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String, java.lang.String)">SQLiteDatabase query method</a>.</p>

        <div class="bs-docs-example">
{% highlight java %}
Note.find(Note.class, "name = ? and title = ?", "satya", "title1");
{% endhighlight %}
        </div>

        <p class="lead">If you have other conditions like group by, order by or limit, you could use the following method on the domain entity:</code>

        <div class="bs-docs-example">
{% highlight java %}
find(Class<T> type, String whereClause, String[] whereArgs, String groupBy, String orderBy, String limit)
{% endhighlight %}
        </div>

        <p class="lead">In case you're looking to execute a custom query, you could use the following methods:</code>

        <div class="bs-docs-example">
{% highlight java %}
// Could execute other raw queries too here..
Note.executeQuery("VACUUM");

// for finders using raw query.
List<Note> notes = Note.findWithQuery(Note.class, "Select * from Note where name = ?", "satya");
{% endhighlight %}
        </div>

        <p class="lead">With a Query Builder approach, you could do the following:</p>

        <div class="bs-docs-example">
{% highlight java %}
Select.from(TestRecord.class)
.where(Condition.prop("test").eq("satya"),
Condition.prop("prop").eq(2))
.list();
{% endhighlight %}
        </div>
        <p class="lead">Property names are not converted currently. So, it'd be the table column names (conversion eg: testUnderscore => test_underscore). You could fine few more examples in the <a href="https://github.com/satyan/sugar/blob/master/library/test/com/orm/query/SelectTest.java">SelectTest.java</a> class.
        </p>
      </section>

    </div>
  </div>
</div>
